package com.br.android.widingwheels;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.br.android.widingwheels.db.WidingWheelsDAO;
import com.br.android.widingwheels.entidades.Gasto;
import com.br.android.widingwheels.maps.MapGastosActivity;

@SuppressLint("NewApi")
public class AdicionarGastoActivity extends SherlockActivity{

	private WidingWheelsDAO dao;
	
	private EditText editTextNomeGasto;
	private EditText editTextValorGasto;
	private Spinner spinnerCategoria;
	private CheckBox checkBoxLocal;
	
	private Gasto gasto;
	
	private SimpleDateFormat formatData;
	
	private Intent intentRetorno;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

//		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
//		StrictMode.setThreadPolicy(policy); 
		
		intentRetorno = new Intent();
		
		setContentView(R.layout.layout_adicionar_gasto);
	
		dao = new WidingWheelsDAO(this);
		gasto = new Gasto();
		formatData = new SimpleDateFormat("dd/MM/yyyy HH:mm");
		
		editTextNomeGasto = (EditText) findViewById(R.id.editTextNomeGasto);
		editTextValorGasto = (EditText) findViewById(R.id.editTextValorGasto);
		spinnerCategoria = (Spinner) findViewById(R.id.spinnerCategoria);
		checkBoxLocal = (CheckBox) findViewById(R.id.checkBoxCompartilhaLocal);
		
		checkBoxLocal.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(checkBoxLocal.isChecked()){
					startActivityForResult(new Intent(AdicionarGastoActivity.this, ListaLocaisActivity.class), 1);
					overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
				}
			}
		});
		
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == 1){
			intentRetorno = data;
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.menu_adicionar_gasto, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menuAdicionarGasto:
			gasto = new Gasto();
			if(editTextNomeGasto.getText().toString().isEmpty()){
				mensagem("Digite um nome para o gasto.");
			} else if (editTextValorGasto.getText().toString().isEmpty()) {
				mensagem("Digite um valor.");
				
			} else {
				gasto.setNomeGasto(editTextNomeGasto.getText().toString());
				gasto.setValorGasto(Double.parseDouble(editTextValorGasto.getText().toString()));
				gasto.setCategoriaGasto(spinnerCategoria.getSelectedItem().toString());
				gasto.setDataGasto(formatData.format(new Date()));
				if(checkBoxLocal.isChecked()){
					gasto.setNomeLocal(intentRetorno.getExtras().getString("local"));
					gasto.setLatitudeGasto(intentRetorno.getExtras().getDouble("latitude"));
					gasto.setLongitudeGasto(intentRetorno.getExtras().getDouble("longitude"));
					dao.inserirGasto(gasto, true);
				} else {
					dao.inserirGasto(gasto, false);
				}
				startActivity(new Intent(this, PrincipalActivity.class));
				overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
				finish();
			}
			break;
	
		case android.R.id.home:
			startActivity(new Intent(this, PrincipalActivity.class));
			overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	
	public void mensagem(String msg){
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
	
}
